//
//  inputTextFTableViewCell.m
//  
//
//  Created by gynjobs on 2017/8/14.
//
//

#import "inputTextFTableViewCell.h"

@interface inputTextFTableViewCell()<UITextFieldDelegate>


@end

@implementation inputTextFTableViewCell


-(instancetype)init{
    
    if(self = [super initWithStyle:UITableViewCellStyleDefault reuseIdentifier:TEXT_FIELD_CELL_ID]){
        
        self.item = nil;
        self.textDelegate = nil;
        
        self.contentTextfield = [[UITextField alloc] init];
        self.contentTextfield.font = [UIFont systemFontOfSize:UI_FOND_HOME_CREDIT_CELL_MAIN_1];
        self.contentTextfield.textAlignment = NSTextAlignmentRight;
        
        self.contentTextfield.delegate = self;
        [self addSubview:self.contentTextfield];
    }
    
    [self updateConstraintsIfNeeded];
    return self;
    
}

-(instancetype)initWithId:(NSString*)identifier{
    
    if(self = [super initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifier]){
        
        self.item = nil;
        self.textDelegate = nil;
        
        self.contentTextfield = [[UITextField alloc] init];
        self.contentTextfield.font = [UIFont systemFontOfSize:UI_FOND_HOME_CREDIT_CELL_MAIN_1];
        self.contentTextfield.textAlignment = NSTextAlignmentRight;
        
        self.contentTextfield.delegate = self;
        [self addSubview:self.contentTextfield];
    }
    return self;
}


-(void)updateConstraints{
    [super updateConstraints];
    
    [self.contentTextfield mas_remakeConstraints:^(MASConstraintMaker *make) {
        
        make.centerY.equalTo(self.mas_centerY);
        make.left.equalTo(titleLabel.mas_right).with.offset(LEFT_RIGHT_SPACE);
        make.right.equalTo(self.mas_right).with.offset(-LEFT_RIGHT_SPACE);
        make.height.mas_equalTo(CONTENT_TEXTFIELD_HEIGHT);
        
    }];
    
}

//-(void)setIsExpand:(NSInteger)pIsExpand{
//
//    [super setIsExpand:pIsExpand];
//
//    [contentTextfield mas_updateConstraints:^(MASConstraintMaker *make) {
//
//        make.left.equalTo(titleLabel.mas_right).with.offset(LEFT_RIGHT_SPACE);
//    }];
//
//}


#pragma mark -
#pragma mark touch begin

-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{
    
    [self.superview endEditing:YES];
}

#pragma mark -
#pragma mark textfiled delegate

- (void)textFieldDidEndEditing:(UITextField *)textField{
    
    NSString * content = textField.text;
    if(!content){
        
        content = @"";
    }
    
    if(self.textDelegate && [self.textDelegate respondsToSelector:@selector(loanTextField:withData:)]){
        
        NSMutableDictionary * dic = [NSMutableDictionary dictionaryWithCapacity:2];
        
        if([content length] == 0){
            
            [dic setValue:@"0" forKey:@"score"];
            
        }else{
            
            [dic setValue:self.item.tip3 forKey:@"score"];
        }
        
        [dic setValue:content forKey:@"data"];
        [dic setValue:self.item.key forKey:@"key"];
        [self.textDelegate loanTextField:textField withData:dic];
    }
    
}



/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

@end
